package utstat.listeners;

import java.util.HashMap;
import java.util.Map;

import utstat.UtEvent;
import utstat.UtEventListener;
import static utstat.Type.*;
import static utstat.Weapon.*;

/**
 * xxx had an ill-conceived escape route Suicide within 3 seconds of receiving a hit
 * 
 * xxx's reserve chute failed to 2 lemming deaths in a game
 */
public class LemmingListener implements UtEventListener {

	private Map<String, Integer> fallKills = new HashMap<String, Integer>();
	private Map<String, Integer> lastHitTimes = new HashMap<String, Integer>();
	private int currentRound = -1;

	@Override
	public String getIntestingFact(String player) {
		return null;
	}

	@Override
	public void processEvent(UtEvent e) {
		if (e.getRound() != currentRound) {
			nextRound();
			currentRound = e.getRound();
		}
		if (e.getType() == hit) {
			lastHitTimes.put(e.getSrc(), e.getTime());
		}

		if (e.getType() == kill && (e.getWeapon() == Falling || e.getWeapon() == TriggerHurt)) {

			increment(e.getDest(), fallKills);

			int falls = fallKills.get(e.getDest());

			if (falls == 4) {
				System.out.println(falls+" "+e.getDest() + "'s reserve chute failed too...");
			}

			Integer lastHitTime = lastHitTimes.get(e.getDest());
			if (lastHitTime != null && e.getTime() - lastHitTime < 4) {
				System.out.println(e.getDest() + " had an ill-conceived escape route");
			}
		}
	}

	private int increment(String player, Map<String, Integer> countMap) {
		Integer count = countMap.get(player);
		if (count == null) count = 0;
		count++;
		countMap.put(player, count);
		return count;
	}

	private void nextRound() {
		System.out.println("------");
		lastHitTimes = new HashMap<String, Integer>();
		fallKills = new HashMap<String, Integer>();
	}

}
